package com.xoms.admin.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.ResourceBean;
import com.xoms.admin.model.vo.HaveResourcesBean;
import com.xoms.admin.persist.dao.ResourceDao;
import com.xoms.util.StringUtil;

@Repository
public class ResourceImpl extends BaseImpl implements ResourceDao {
	
	final String NAME_SPACE = NAME_SPACE_HEADER + ".ResourceMapper";

	@Override
	public List<HaveResourcesBean> selectResources2Role(int roleId) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectResources2Role", roleId);
	}

	@Override
	public int selectCount(String key) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectCount", params);

	}

	@Override
	public List<ResourceBean> selectList(String key, int page, int pagesize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pagesize);
		params.put("length", pagesize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}

		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);

	}

	@Override
	public int updateStatus(int rid, int status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("rid", rid);
		params.put("status", status);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateStatus", params);
	
	}

	@Override
	public int del(int rid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete", rid);
	}

	@Override
	public int insert(ResourceBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(ResourceBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}

	@Override
	public ResourceBean selectOneById(Integer id) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", id);
	}

	@Override
	public int selectRoleNum2Resource(int rid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectRoleNum2Resource", rid);
	}

	@Override
	public int delAllResourceRelation(int rid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delAllResourceRelation", rid);
	}

	@Override
	public int updateSorts(List<String> sortList, int page, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		params.put("sortList", sortList);
		
		return sqlSessionTemplate.update(NAME_SPACE + ".updateSorts", params);
	
	}



}
